package com.example.service.impl;

import com.example.entity.Storage;
import com.example.mapper.StorageMapper;
import com.example.service.StorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author: 21050134
 * @date: 2023/10/20 15:46
 * @description:
 */
@Service
@Slf4j
public class StorageServiceImpl implements StorageService {
	@Resource
	private StorageMapper storageMapper;

	@Override
	public int decrease(Long productId,Integer count){
		log.info("--------->storage-service 中扣减库存开始");
		log.info("------->storage-service 开始查询商品是否存在");
		Storage storage = storageMapper.selectByProductId(productId);
		if (storage !=null && storage.getResidue().intValue() >= count.intValue()) {
		Storage storage1 = new Storage();
		storage1.setProductId(productId);
		storage1.setUsed(storage.getUsed() + count);
		storage1.setResidue(storage.getTotal().intValue() - storage1.getUsed());
		int decrease = storageMapper.decrease(storage1);
		log.info("------->storage-service 扣减库存成功");
		return decrease;
		}else{
			log.info("------->storage-service 库存不足，开始回滚！");
			throw new RuntimeException("库存不足，扣减库存失败！");
		}
	}

}
